import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import com.toedter.calendar.JDateChooser;
import javax.swing.JComboBox;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextArea;
import java.io.*;
import java.text.Format;
import java.util.*;
import java.text.SimpleDateFormat;


public class appointment {
	
	private JFrame frame;
	public JTextField locationText;
	public JTextField eventName;
	public JTextArea descriptionText;
	public JButton btnNewButton;
	public JComboBox comboBox;
	public JComboBox comboBox_1;
	public JDateChooser dateChooser;
	public JDateChooser dateChooser_1;
	
	public JTextField numberField_2;
	public JTextField numberField_1;
	
	
	public void setDateToToday() {
		java.util.Date d = new java.util.Date();
		//testDate(d);
		dateChooser.setDate(d);
		dateChooser_1.setDate(d);
	}
	
	public void testDate(Date d) {
		d.clone();
	}
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					appointment window = new appointment();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			
		});
		//question here
		
	}

	/**
	 * Create the application.
	 */
	public appointment() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		String[] timeStrings = { "Start Time","12:00 AM","12:30 AM","1:00 AM","1:30 AM","2:00 AM","2:30 AM","3:00 AM","3:30 AM","4:00 AM","4:30 AM","5:00 AM","5:30 AM","6:00 AM",
								"6:30 AM","7:00 AM","7:30 AM","8:00 AM","8:30 AM","9:00 AM","9:30 AM","10:00 AM","10:30 AM","11:00 AM","11:30 AM","12:00 PM","12:30 PM","1:00 PM",
								"1:30 PM","2:00 PM","2:30 PM","3:00 PM","3:30 PM","4:00 PM","4:30 PM","5:00 PM","5:30 PM","6:00 PM","6:30 PM","7:00 PM","7:30 PM","8:00 PM","8:30 PM",
								"9:00 PM","9:30 PM","10:00 PM","10:30 PM","11:00 PM","11:30 PM"};
		String[] timeStrings1 = { "End Time","12:00 AM","12:30 AM","1:00 AM","1:30 AM","2:00 AM","2:30 AM","3:00 AM","3:30 AM","4:00 AM","4:30 AM","5:00 AM","5:30 AM","6:00 AM",
								 "6:30 AM","7:00 AM","7:30 AM","8:00 AM","8:30 AM","9:00 AM","9:30 AM","10:00 AM","10:30 AM","11:00 AM","11:30 AM","12:00 PM","12:30 PM","1:00 PM",
								 "1:30 PM","2:00 PM","2:30 PM","3:00 PM","3:30 PM","4:00 PM","4:30 PM","5:00 PM","5:30 PM","6:00 PM","6:30 PM","7:00 PM","7:30 PM","8:00 PM","8:30 PM",
								 "9:00 PM","9:30 PM","10:00 PM","10:30 PM","11:00 PM","11:30 PM"};
		
		frame = new JFrame();
		frame.getContentPane().setFont(new Font("Tahoma", Font.PLAIN, 12));
		frame.setBounds(100, 100, 418, 361);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JLabel lblEventName = new JLabel("Event Name:");
		lblEventName.setFont(new Font("Tahoma", Font.PLAIN, 12));
		
		JLabel lblStartTime = new JLabel("Start Time:");
		lblStartTime.setFont(new Font("Tahoma", Font.PLAIN, 12));
		
		JLabel lblEndTime = new JLabel("End Time:");
		lblEndTime.setFont(new Font("Tahoma", Font.PLAIN, 12));
		
		JLabel lblLocation = new JLabel("Location:");
		lblLocation.setFont(new Font("Tahoma", Font.PLAIN, 12));
		
		JLabel lblDescriptions = new JLabel("Descriptions:");
		lblDescriptions.setFont(new Font("Tahoma", Font.PLAIN, 12));
		
		locationText = new JTextField();
		locationText.setColumns(10);
		
		dateChooser = new JDateChooser();
		dateChooser_1 = new JDateChooser();
		
		eventName = new JTextField();
		eventName.setColumns(10);
		
		numberField_1 = new JTextField();
		numberField_2 = new JTextField();
		
		comboBox = new JComboBox(timeStrings);
		comboBox.setFont(new Font("Tahoma", Font.PLAIN, 12));
		comboBox.setSelectedIndex(0);
		comboBox_1 = new JComboBox(timeStrings1);
		comboBox_1.setFont(new Font("Tahoma", Font.PLAIN, 12));
		comboBox_1.setSelectedIndex(0);
		btnNewButton = new JButton("Create Event");
		
		//eventHandler
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Date startDate = dateChooser_1.getDate();
				Date endDate = dateChooser.getDate();
				Format format = new SimpleDateFormat("yyyyMMdd");
				//Format format1 = new SimpleDateFormat("hhmmssa"
				Format tzformat = new SimpleDateFormat("Z");
				String startTime = (String) comboBox.getSelectedItem();
                String endTime = (String) comboBox_1.getSelectedItem();
				int hour = Integer.parseInt(startTime.substring(0,startTime.indexOf(':')));
				String minute = startTime.substring(startTime.indexOf(':')+1,startTime.indexOf(' '));
				int aOrp = startTime.indexOf("M")-1;
				  //Integer.parseInt(startTime.substring(startTime.indexOf("M")-1));
				
				if(startTime.charAt(aOrp) == 'P'){
				  hour = hour + 12;
				} //end if
				
				int hour1 = Integer.parseInt(endTime.substring(0,endTime.indexOf(':')));
                String minute1 = endTime.substring(endTime.indexOf(':')+1,endTime.indexOf(' '));
                int aOrp1 = endTime.indexOf("M")-1;
                  //Integer.parseInt(startTime.substring(startTime.indexOf("M")-1));
                
                if(startTime.charAt(aOrp1) == 'P'){
                  hour1 = hour1 + 12;
                } //end if
				//String dt2 = TimeShift.formatTime(date,"hh:mm");
				System.out.println(startTime);
				System.out.println(endTime);
				String startHour = (hour <10) ? "0"+ hour : "" + hour;
				String endHour = (hour1 <10) ? "0"+ hour1 : "" + hour1;
				
				
				try {
		            BufferedWriter bWriter = new BufferedWriter(new FileWriter("test." +
		            		"ics"));
		        
					bWriter.write("BEGIN:VCALENDAR");
					bWriter.newLine();
					bWriter.write("VERSION:1.0");
					bWriter.newLine();
					bWriter.write("BEGIN:VTIMEZONE");
					bWriter.newLine();
					bWriter.write("TZID:"+tzformat.format(startDate));
					bWriter.newLine();
					bWriter.write("END:VTIMEZONE");
					bWriter.newLine();
					bWriter.write("BEGIN:VEVENT");
					bWriter.newLine();
					bWriter.write("CLASS:PUBLIC");
					bWriter.newLine();
					bWriter.write("GEO:"+numberField_1.getText() + ";" + numberField_2.getText());
					bWriter.newLine();
					bWriter.write("DTSTART:" + format.format(startDate) + "T" + startHour + minute + "00");
					bWriter.newLine();
					bWriter.write("DTEND:" + format.format(endDate) + "T" + endHour +minute1 + "00");
					bWriter.newLine();
					bWriter.write("SUMMARY:" + eventName.getText());
					bWriter.newLine();
					bWriter.write("LOCATION:" + locationText.getText());
					bWriter.newLine();
					bWriter.write("DESCRIPTION:" + descriptionText.getText());
					bWriter.newLine();
					bWriter.write("END:VEVENT");
					bWriter.newLine();
					bWriter.write("END:VCALENDAR");
					bWriter.close();
				} catch (IOException e1) {
                  // TODO Auto-generated catch block
                  e1.printStackTrace();
              }
			}
		});  //end of eventHandler
		btnNewButton.setFont(new Font("Tahoma", Font.PLAIN, 12));
		
		descriptionText = new JTextArea();
		
		numberField_2 = new JTextField();
		numberField_2.setColumns(10);
		
		numberField_1 = new JTextField();
		numberField_1.setColumns(10);
		
		JLabel lblLatlon = new JLabel("Lat/Lon");
		GroupLayout groupLayout = new GroupLayout(frame.getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
						.addComponent(lblEventName)
						.addGroup(groupLayout.createSequentialGroup()
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addComponent(lblStartTime)
								.addComponent(lblDescriptions)
								.addComponent(lblLocation)
								.addComponent(lblEndTime)
								.addComponent(lblLatlon))
							.addGap(29)
							.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING, false)
								.addGroup(groupLayout.createSequentialGroup()
									.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
										.addComponent(dateChooser, GroupLayout.DEFAULT_SIZE, 123, Short.MAX_VALUE)
										.addComponent(dateChooser_1, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 123, Short.MAX_VALUE))
									.addGap(41)
									.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
										.addComponent(comboBox, GroupLayout.PREFERRED_SIZE, 121, GroupLayout.PREFERRED_SIZE)
										.addComponent(comboBox_1, GroupLayout.PREFERRED_SIZE, 121, GroupLayout.PREFERRED_SIZE)))
								.addGroup(groupLayout.createSequentialGroup()
									.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
										.addComponent(locationText, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 256, Short.MAX_VALUE)
										.addComponent(eventName, GroupLayout.DEFAULT_SIZE, 256, Short.MAX_VALUE)
										.addGroup(groupLayout.createSequentialGroup()
											.addComponent(numberField_1, GroupLayout.PREFERRED_SIZE, 82, GroupLayout.PREFERRED_SIZE)
											.addPreferredGap(ComponentPlacement.RELATED)
											.addComponent(numberField_2, GroupLayout.PREFERRED_SIZE, 88, GroupLayout.PREFERRED_SIZE)
											.addGap(24)
											.addComponent(btnNewButton))
										.addComponent(descriptionText, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 285, Short.MAX_VALUE))
									.addPreferredGap(ComponentPlacement.RELATED)))))
					.addGap(201))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(21)
					.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
						.addComponent(lblEventName)
						.addComponent(eventName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(15)
					.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
						.addComponent(dateChooser_1, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
						.addComponent(comboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblStartTime))
					.addGap(13)
					.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
						.addComponent(dateChooser, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
						.addComponent(comboBox_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblEndTime))
					.addGap(11)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblLocation)
						.addComponent(locationText, GroupLayout.PREFERRED_SIZE, 22, GroupLayout.PREFERRED_SIZE))
					.addGap(27)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(lblDescriptions)
						.addComponent(descriptionText, GroupLayout.PREFERRED_SIZE, 83, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnNewButton, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
						.addComponent(lblLatlon)
						.addComponent(numberField_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(numberField_2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(84))
		);
		frame.getContentPane().setLayout(groupLayout);
	}
}
